import Koa from "koa";
import log from "koa-log";
import cors from "@koa/cors";
import router from "src/routes";
import bodyParser from "koa-bodyparser";
import middlewares from "src/middlewares";
import initEnv from "src/config/env";
import initDatabase from "src/config/database";

initEnv();
initDatabase();

const app = new Koa();

app.use(cors());
app.use(bodyParser());
app.use(middlewares);
app.use(router.routes());
app.use(router.allowedMethods());
app.use(log(process.env.MODE));

app.listen(process.env.PORT, () => {
  console.log(`Listening on http://localhost:${process.env.PORT}`);
});

app.on("error", (err) => {
  console.error("Server error", err);
});
